<?php
class Creditdao extends Controller{
	
	public static function napCredit_LR($user, $soluong, $ngaynap){
		$cre = Credit::model()->findByAttributes(array('iduser'=>$user->id));
		if($cre === null){
			$cre = new Credit;
			$cre->iduser = $user->id;
			$cre->username = $user->username;
			$cre->sodu =0;
		}
		
		$cre->sodu = $cre->sodu + $soluong;
		$cre->save();
		
		$chitiet = new Chitietnapcredit;
		$chitiet->idcredit = $cre->id;
		$chitiet->ngaynap = $ngaynap;
		$chitiet->soluong = $soluong;
		$chitiet->hinhthucnap = "Nạp LR";
		$chitiet->save();
		return $chitiet;
	}
	
	public static function napTheCao($user, $menhgia){
		
		$cre = Credit::model()->findByAttributes(array('iduser'=>$user->id));
		if($cre === null){
			$cre = new Credit;
			$cre->iduser = $user->id;
			$cre->username = $user->username;
			$cre->sodu =0;			$cre->tongnap=0;
		}
		
		$cre->sodu = $cre->sodu + $menhgia/25000;		$cre->tongnap = $cre->tongnap + $menhgia/25000;
		$cre->save();
		
		$chitiet = new Chitietnapcredit;
		$chitiet->idcredit = $cre->id;
		$chitiet->ngaynap = DateUtils::getNow();
		$chitiet->soluong = $menhgia/25000;
		$chitiet->hinhthucnap ="Nạp thẻ cào mệnh giá ". $menhgia;
		$chitiet->save();
		return $chitiet;
	}
	
	public static function creditAvailable($iduser){
		$cre = Credit::model()->findByAttributes(array('iduser'=>$iduser));
		if($cre === null){
			return 0;
		}
		return $cre->sodu;
	}
	
	/**
	 * Them credit cho user
	 * Enter description here ...
	 * @param $user
	 * @param $cre
	 */
	public static function addCredit($user, $cre){
		$credit = Credit::model()->findByAttributes(array('iduser'=>$user->id));
		if($credit == null){
			$credit = new Credit;
			$credit->iduser = $user->id;
			$credit->sodu = $cre;
			$credit->tongnap = $cre;
			$credit->dasai = 0;
			$credit->tamgiu = 0;
			$credit->save();
		}else{
			$credit->sodu = $credit->sodu + $cre;
			$credit->tongnap = $credit->tongnap + $cre;
			$credit->update();
		}
		
		//luu lai lich su
		$chitiet = new Chitietnapcredit;
		$chitiet->idcredit = $credit->id;
		$chitiet->ngaynap = DateUtils::getNow();
		$chitiet->soluong = $cre;
		$chitiet->hinhthucnap ="Nạp bởi admin";
		if($cre < 0){
			$chitiet->hinhthucnap ="Trừ bởi admin";
		}
		$chitiet->save();
		return $chitiet;
	}
	
	public static function truCredit($credit, $soluong, $user, $noidung){
		$credit->sodu = $credit->sodu - $soluong;
		$credit->tamgiu= $credit->tamgiu + $soluong;
		$credit->update();
		
		$chitiet = new Chitietnapcredit;
		$chitiet->idcredit = $credit->id;
		$chitiet->ngaynap = DateUtils::getNow();
		$chitiet->soluong = -$soluong;
		$chitiet->hinhthucnap ="Mua code: ". $noidung;
		$chitiet->save();
		EmailUtils::sendemailCreditChange($chitiet, $user);
		return $chitiet;
	}
	
	public static function CreateEmptyCredit(){
		$user = Yii::app()->user->getId();
		if($user != null){
			$credit = Credit::model()->findByAttributes(array('iduser'=>$user->id));
			if($credit == null){
				$credit = new Credit;
				$credit->iduser = $user->id;
				$credit->username = $user->username;
				$credit->sodu = 0;
				$credit->save();
			}
		}
	}
	
	public static function HistoryNapCre($iduser){
		$user = User::model()->findByPK($iduser);
		
		$criteria = new CDbCriteria();
		$criteria->compare('idcredit', $user->tbcredit->id);
		$Page = new CPagination(Chitietnapcredit::model()->count($criteria));
		$criteria->order = 'ngaynap DESC';
		$Page->pageSize = 25;
		$Page->applyLimit($criteria);
		$data['history'] =  Chitietnapcredit::model()->findAll($criteria);
		$data['pages'] = $Page;

		return $data;
	}
}